'''
Created on Nov 14, 2011

@author: marcosvariz
'''
import pygame
from SimFloresta import *
from InfoWindow import *

#Variaveis de ambiente
X=0
Y=1
GRID_SIZE = (160,90)
UPDATE_TIME = 10
FRAME_RATE = 70

#Iniciar pygame, janela principal e relogio
WINDOW_SIZE = (GRID_SIZE[X] * PATCH_SIZE[X],GRID_SIZE[Y] * PATCH_SIZE[Y])
pygame.init()
SCREEN = pygame.display.set_mode(WINDOW_SIZE)
clock = pygame.time.Clock()
SCREEN.fill((0,0,0))
#Iniciar Janela de Info
info = InfoWindow(SCREEN)

#Iniciar janela secundaria


#Iniciar Floresta
floresta = SimFloresta(SCREEN,GRID_SIZE)
floresta.setupInit(100)
pygame.display.flip()

#Contadores e variaveis de controle
estacaoPrimavera = True
ano = 0
pause = True
updateClick = pygame.time.get_ticks()
infoActivo = False

#Editar Grelha Inicial com comandos do Rato
contEdit=True
while pause:
    for event in pygame.event.get():
        if event.type == pygame.MOUSEBUTTONDOWN:
            crd = pygame.mouse.get_pos()
            floresta.setPatch((crd[X]*GRID_SIZE[X]/WINDOW_SIZE[X],crd[Y]*GRID_SIZE[Y]/WINDOW_SIZE[Y]))
            pygame.display.flip()
        elif event.type == pygame.KEYDOWN:
            pause = False



#Ciclo principal

while True:
    #Detectar eventos
    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN:
            pause = not pause
        elif event.type == pygame.QUIT:
            exit()
        elif event.type == pygame.MOUSEBUTTONDOWN:
            crd = pygame.mouse.get_pos()
            floresta.setPatch((crd[X]*GRID_SIZE[X]/WINDOW_SIZE[X],crd[Y]*GRID_SIZE[Y]/WINDOW_SIZE[Y]))
            pygame.display.flip()
        
    
    #Update da Floresta
    if not pause:
        if pygame.time.get_ticks()-updateClick >= UPDATE_TIME:
            if estacaoPrimavera:
                #Primavera
                ano += 1
                print ano
                floresta.updateStatesPrimavera()
                estacaoPrimavera = False
            else:
                #Verao
                floresta.updateStatesVerao()
                estacaoPrimavera = True
                
            updateClick = pygame.time.get_ticks()
            if infoActivo:
                info.desenhar()
            pygame.display.flip()
    
        
        
    #Controlar frame rate    
    time_passed = clock.tick(FRAME_RATE)
    
   

    
